﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web;

namespace HardwarePlatform.SMS.Business
{
    /// <summary>
    /// 奇致qz876运营商
    /// </summary>
    public class Qz876 : SMSInterface
    {
        private string account = "871604";
        private string pwd = Convert.ToBase64String(System.Text.Encoding.Default.GetBytes("zm123456789"));//密码要Base64编码
        string urltem = "http://61.147.98.117:9001/servlet/UserServiceAPI";
        string sendmethod = "sendSMS";
        string balancemethod = "getRestMoney";
        //public 
        public Qz876()
        {

        }
        /// <summary>
        /// 查询余额
        /// </summary>
        /// <returns></returns>
        public string QueryBalance()
        {
            string result = "";
            string pwdz = Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(pwd));
            string strurl = urltem + "?method=" + balancemethod + "&username=" + account + "&password=" + pwdz;
            WebRequest myRequest = WebRequest.Create(strurl);
            WebResponse myResponse = myRequest.GetResponse();
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.GetEncoding("GBK"));
            string tem = reader.ReadToEnd();
            if (!string.IsNullOrEmpty(tem) && (tem.IndexOf("gateSmsNum") != -1))
                result = tem.Split(';')[1].Split('=')[1];
            else
                result = tem;
            return result;
        }

        public bool SavePhones(string SendResult, string Phones, string Content)
        {
            throw new NotImplementedException();
        }

        public string Send(string Phones, string Content)
        {
            string result = "";
            string pwdz = Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(pwd));
            string temcontent = "";
            //内容转GBK
            byte[] tembytes = Encoding.GetEncoding("GBK").GetBytes(Content);
            //再转为UrlEncode
            temcontent = HttpUtility.UrlEncode(tembytes);
            string strurl = urltem + "?method=" + sendmethod +
                "&username=" + account + "&password=" + pwdz + "&smstype=1&mobile=" +
                Phones + "&content=" + temcontent;
            WebRequest myRequest = WebRequest.Create(strurl);
            WebResponse myResponse = myRequest.GetResponse();
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
            result = reader.ReadToEnd();
            return result;
        }

        private ThirdPartyOperators GetData()
        {
            return null;
        }
    }
}
